package com.wang.bootbuilder.tools;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/*
统一处理返回结果
 */
@Data
public class JsonResult {

    @ApiModelProperty(value = "返回状态码")
    private Integer status;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    /*
    私有构造方法
     */
    private JsonResult() {}

    /*
    成功
     */
    public static JsonResult ok() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatus(ReponseEnum.SUCCESS.getCode());
        jsonResult.setMessage(ReponseEnum.SUCCESS.getMessage());
        return jsonResult;
    }

    /*
    失败
     */
    public static JsonResult error() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatus(ReponseEnum.ERROR.getCode());
        jsonResult.setMessage(ReponseEnum.ERROR.getMessage());
        return jsonResult;
    }

    /*
    没有权限
     */
    public static JsonResult permission() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatus(ReponseEnum.PERMISSION.getCode());
        jsonResult.setMessage(ReponseEnum.PERMISSION.getMessage());
        return jsonResult;
    }


    public JsonResult message(String message) {
        this.setMessage(message);
        return this;
    }

    public JsonResult status(Integer status) {
        this.setStatus(status);
        return this;
    }

    public JsonResult data (String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public JsonResult data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

}
